package com.jspxcms.plug.service.impl;

import com.jspxcms.common.orm.Limitable;
import com.jspxcms.common.orm.RowSide;
import com.jspxcms.common.orm.SearchFilter;
import com.jspxcms.plug.domain.PlugHospital;
import com.jspxcms.plug.repository.PlugHospitalDao;
import com.jspxcms.plug.service.PlugHospitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class PlugHospitalServiceImpl implements PlugHospitalService {
    public Page<PlugHospital> findAll(Integer siteId, Map<String, String[]> params,
                                      Pageable pageable) {
        return dao.findAll(spec( params), pageable);
    }

    public RowSide<PlugHospital> findSide(Integer siteId,
                                          Map<String, String[]> params, PlugHospital bean, Integer position,
                                          Sort sort) {
        if (position == null) {
            return new RowSide<PlugHospital>();
        }
        Limitable limit = RowSide.limitable(position, sort);
        List<PlugHospital> list = dao.findAll(spec( params), limit);
        return RowSide.create(list, bean);
    }

    private Specification<PlugHospital> spec(
                                             Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        final Specification<PlugHospital> sp = SearchFilter.spec(filters,
                PlugHospital.class);

        return sp;
    }

    public List<PlugHospital> findList(Limitable limitable) {
        return dao.getList(null,limitable);
    }

    public PlugHospital get(Integer id) {
        return dao.findOne(id);
    }

    @Override
    public PlugHospital save(PlugHospital bean) {
        bean = dao.save(bean);
        return bean;
    }

    @Transactional
    public PlugHospital update(PlugHospital bean) {

        bean = dao.save(bean);
        return bean;
    }

    @Transactional
    public PlugHospital delete(Integer id) {
        PlugHospital entity = dao.findOne(id);
        dao.delete(entity);
        return entity;
    }

    @Transactional
    public PlugHospital[] delete(Integer[] ids) {
        PlugHospital[] beans = new PlugHospital[ids.length];
        for (int i = 0; i < ids.length; i++) {
            beans[i] = delete(ids[i]);
        }
        return beans;
    }


    private PlugHospitalDao dao;

    @Autowired
    public void setDao(PlugHospitalDao dao) {
        this.dao = dao;
    }
}
